import Vue from 'vue'
import Router from 'vue-router'
import Vuex from 'vuex'
import index from '@/components/index'
import login from '@/components/login'
import admin from '@/components/admin'
import regist from "../components/regist";

import student from '@/components/student/student'
import studentcourse from "../components/student/studentcourse";
import studentcoursecourse from "../components/student/studentcoursecourse";
import studentcoursecourseall from "../components/student/studentcoursecourseall";
import studentcoursecoursestart from "../components/student/studentcoursecoursestart";
import studentcoursecourseing from "../components/student/studentcoursecourseing";
import studentcoursecourseend from "../components/student/studentcoursecourseend";
import studentcoursecollection from "../components/student/studentcoursecollection";
import studentcoursetest from "../components/student/studentcoursetest";
import studentcoursetestchapter from "../components/student/studentcoursetestchapter";
import studentcoursetestfinal from "../components/student/studentcoursetestfinal";
import studentcoursetestdiscussion from "../components/student/studentcoursetestdiscussion";
import studentorder from "../components/student/studentorder";
import studentorderbuy from "../components/student/studentorderbuy";
import studentorderrefund from "../components/student/studentorderrefund";
import studentsetup from "../components/student/studentsetup";
import studentsetupinfo from "../components/student/studentsetupinfo";
import studentsetupinfostudent from "../components/student/studentsetupinfostudent";
import studentsetupinfouser from "../components/student/studentsetupinfouser";
import studentsetupmessage from "../components/student/studentsetupmessage";
import studentsetupprivacy from "../components/student/studentsetupprivacy";
import studentsetuppay from "../components/student/studentsetuppay";
import studentmessage from "../components/student/studentmessage";
import studentmessageall from "../components/student/studentmessageall";
import studentmessagesystem from "../components/student/studentmessagesystem";
import studentmessageannouncement from "../components/student/studentmessageannouncement";
import studentmessageletter from "../components/student/studentmessageletter";


import teacher from '@/components/teacher/teacher'
import teacher_course from "../components/teacher/teacher_course";
import teacher_courseall from "../components/teacher/teacher_courseall";
import teacher_courselive from "../components/teacher/teacher_courselive";
import teacher_coursecourse from "../components/teacher/teacher_coursecourse";
import teacher_coursepublish from "../components/teacher/teacher_coursepublish";
import teacher_coursepublishchapter from "../components/teacher/teacher_coursepublishchapter";
import teacher_coursepublishsection from "../components/teacher/teacher_coursepublishsection";
import teacher_info from "../components/teacher/teacher_info";
import teacher_income from "../components/teacher/teacher_income";
import teacher_inincome from "../components/teacher/teacher_inincome";
import teacher_inpay from "../components/teacher/teacher_inpay";
import teacher_set from "../components/teacher/teacher_set";
import teacher_setauth from "../components/teacher/teacher_setauth";
import teacher_setinfo from "../components/teacher/teacher_setinfo";
import teacher_setpayment from "../components/teacher/teacher_setpayment";
import teacher_setphone from "../components/teacher/teacher_setphone";

import course from '@/components/course/allcoursedisplayslot'
import coursemain from '@/components/course/coursemain'
import courseHome from '@/components/course/courseHome'
import recruit from '@/components/course/recruit'
import aboutUs from '@/components/course/aboutUs'
import buyNow from '@/components/course/buyNow'
import aaa from '@/components/course/aaa'

import store from "../vuex/index"




//Vue 重复进入相同路由消除警报
const originalReplace = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalReplace.call(this, location).catch(err => err)
}
Vue.use(Router)
Vue.use(Vuex)


let router= new Router({
  routes: [
    {
      path: '/',
      name: 'index',
      component: index
    },
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/regist',
      name: 'regist',
      component: regist
    },
    {
      path: '/admin',
      name: 'admin',
      component: admin,
      meta:{
        //自定义属性指定访问该路由必须有admin角色
        role: 'admin',
        // 添加该字段，表示进入这个路由是需要登录的
        requireAuth: true
      }
    },
    {
      path: '/student',
      name: 'student',
      component: student,
      meta:{
        //自定义属性指定访问该路由必须有admin角色
        role: 'student',
        // 添加该字段，表示进入这个路由是需要登录的
        requireAuth: true
      },
      children: [
        {
          path: 'studentcourse',
          name: 'studentcourse',
          component: studentcourse,
          children: [
            {
              path: 'studentcoursecourse',
              name: 'studentcoursecourse',
              component: studentcoursecourse,
              children:[
                {
                  path: 'studentcoursecourseall',
                  name: 'studentcoursecourseall',
                  component: studentcoursecourseall,
                  meta:{
                    //自定义属性指定访问该路由必须有student角色
                    role: 'student',
                    // 添加该字段，表示进入这个路由是需要登录的
                    requireAuth: true
                  }
                },
                {
                  path: 'studentcoursecoursestart',
                  name: 'studentcoursecoursestart',
                  component: studentcoursecoursestart,
                },
                {
                  path: 'studentcoursecourseing',
                  name: 'studentcoursecourseing',
                  component: studentcoursecourseing,
                },
                {
                  path: 'studentcoursecourseend',
                  name: 'studentcoursecourseend',
                  component: studentcoursecourseend,
                },

                {
                  path: '/',
                  redirect:'studentcoursecourseall'
                }
              ]
            },
            {
              path: 'studentcoursecollection',
              name: 'studentcoursecollection',
              component: studentcoursecollection
            },
            {
              path: 'studentcoursetest',
              name: 'studentcoursetest',
              component: studentcoursetest,
              children: [
                {
                  path: 'studentcoursetestchapter',
                  name: 'studentcoursetestchapter',
                  component: studentcoursetestchapter,
                },
                {
                  path: 'studentcoursetestfinal',
                  name: 'studentcoursetestfinal',
                  component: studentcoursetestfinal,
                },
                {
                  path: 'studentcoursetestdiscussion',
                  name: 'studentcoursetestdiscussion',
                  component: studentcoursetestdiscussion,
                },
                {
                  path: '/',
                  redirect:'studentcoursetestchapter'
                }
              ]
            },
            {
              path: '/',
              redirect:'studentcoursecourse'
            }
          ],
        },
        {
          path: 'studentorder',
          name: 'studentorder',
          component: studentorder,
          children: [
            {
              path: 'studentorderbuy',
              name: 'studentorderbuy',
              component: studentorderbuy
            },
            {
              path: 'studentorderrefund',
              name: 'studentorderrefund',
              component: studentorderrefund
            },
            {
              path: '/',
              redirect:'studentorderbuy'
            }
          ],
        },
        {
          path: 'studentsetup',
          name: 'studentsetup',
          component: studentsetup,
          children: [
            {
              path: 'studentsetupinfo',
              name: 'studentsetupinfo',
              component: studentsetupinfo,
              children: [
                {
                  path: 'studentsetupinfostudent',
                  name: 'studentsetupinfostudent',
                  component: studentsetupinfostudent
                },
                {
                  path: 'studentsetupinfouser',
                  name: 'studentsetupinfouser',
                  component: studentsetupinfouser
                },
                {
                  path: '/',
                  redirect:'studentsetupinfostudent'
                }
              ]
            },

            {
              path: 'studentsetupmessage',
              name: 'studentsetupmessage',
              component: studentsetupmessage
            },
            {
              path: 'studentsetupprivacy',
              name: 'studentsetupprivacy',
              component: studentsetupprivacy
            },
            {
              path: 'studentsetuppay',
              name: 'studentsetuppay',
              component: studentsetuppay
            },
            {
              path: '/',
              redirect:'studentsetupinfo'
            }
          ],
        },
        {
          path: 'studentmessage',
          name: 'studentmessage',
          component: studentmessage,
          children: [
            {
              path: 'studentmessageall',
              name: 'studentmessageall',
              component: studentmessageall,
            },
            {
              path: 'studentmessagesystem',
              name: 'studentmessagesystem',
              component: studentmessagesystem,
            },
            {
              path: 'studentmessageannouncement',
              name: 'studentmessageannouncement',
              component: studentmessageannouncement,
            },
            {
              path: 'studentmessageletter',
              name: 'studentmessageletter',
              component: studentmessageletter,
            },
            {
              path: '/',
              redirect: 'studentmessageall'
            }
          ]
        },
        {
          path: '/',
          redirect: 'studentcourse'
        }
      ],
    },
    {
      path: '/teacher',
      name: 'teacher',
      component: teacher,
      meta:{
        //自定义属性指定访问该路由必须有admin角色
        role: 'teacher',
        // 添加该字段，表示进入这个路由是需要登录的
        requireAuth: true
      },
      children: [
        {
          path: 'teacher_course',
          name: 'teacher_course',
          component: teacher_course,
          children: [
            {
              path: 'teacher_courseall',
              name: 'teacher_courseall',
              component: teacher_courseall,
              meta:{
                //自定义属性指定访问该路由必须有admin角色
                role: 'teacher',
                // 添加该字段，表示进入这个路由是需要登录的
                requireAuth: true
              },
            },
            {
              path: 'teacher_courselive',
              name: 'teacher_courselive',
              component: teacher_courselive
            },
            {
              path: 'teacher_coursecourse',
              name: 'teacher_coursecourse',
              component: teacher_coursecourse
            },
            {
              path: 'teacher_coursepublish',
              name: 'teacher_coursepublish',
              component: teacher_coursepublish
            },
            {
              path: 'teacher_coursepublishchapter',
              name: 'teacher_coursepublishchapter',
              component: teacher_coursepublishchapter
            },
            {
              path: 'teacher_coursepublishsection',
              name: 'teacher_coursepublishsection',
              component: teacher_coursepublishsection
            },

          ]
        },
        {
          path: 'teacher_info',
          name: 'teacher_info',
          component: teacher_info
        },
        {
          path: 'teacher_income',
          name: 'teacher_income',
          component: teacher_income,
          children: [
            {
              path: 'teacher_inincome',
              name: 'teacher_inincome',
              component: teacher_inincome
            },
            {
              path: 'teacher_inpay',
              name: 'teacher_inpay',
              component: teacher_inpay
            },

          ]
        },
        {
          path: 'teacher_set',
          name: 'teacher_set',
          component: teacher_set,
          children: [
            {
              path: 'teacher_setinfo',
              name: 'teacher_setinfo',
              component: teacher_setinfo
            },
            {
              path: 'teacher_setpayment',
              name: 'teacher_setpayment',
              component: teacher_setpayment
            },
            {
              path: 'teacher_setauth',
              name: 'teacher_setauth',
              component: teacher_setauth
            },
            {
              path: 'teacher_setphone',
              name: 'teacher_setphone',
              component: teacher_setphone
            },
          ]
        },
      ]


    },
    {
      path: '/course',
      name: 'course',
      component: course,
    },
    {
      path: '/coursemain',
      name: 'coursemain',
      component: coursemain,
    },
    {
      path: '/courseHome',
      name: '/courseHome',
      component: courseHome,
    },
    {
      path: '/recruit',
      name: '/recruit',
      component: recruit
    }, {
      path: '/aboutUs',
      name: '/aboutUs',
      component: aboutUs
    }, {
      path: '/buyNow',
      name: '/buyNow',
      component: buyNow
    },
    {
      path: '/aaa',
      name: '/aaa',
      component: aaa
    }

  ],
  mode: 'history'
})
//路由守卫，前置方法
router.beforeEach((to,from,next)=>{
  let requireAuth = to.meta.requireAuth;
  if (requireAuth) {  // 判断该路由是否需要登录权限
    if (window.sessionStorage.getItem("token")) {  // 通过localStorage获取当前的token是否存在
      //已登录
      //判断要切换的路由是否需要指定的角色权限
      console.log(to.path);
      if (to.path.startsWith('/admin')){
        let role=to.meta.role;
        //判断是否有这个角色
        let hasrole=store.state.grades.roles;
        for (let i = 0; i < hasrole.length; i++) {
          if(hasrole[i].rname===role){
            next();
          }else {
            alert("你没有权限访问该页面")
          }
        }
      }else if (to.path.startsWith('/student')){
        let role=to.meta.role;
        //判断是否有这个角色
        let hasrole=store.state.grades.roles;
        for (let i = 0; i < hasrole.length; i++) {
          if(hasrole[i].rname===role){
            next();
          }else {
            alert("你没有权限访问该页面")
          }
        }
      }
      else if (to.path.startsWith('/teacher')){
        let role=to.meta.role;
        //判断是否有这个角色
        let hasrole=store.state.grades.roles;
        for (let i = 0; i < hasrole.length; i++) {
          if(hasrole[i].rname==="teacher"||hasrole[i].rname==="student"){
            next();
            return;
          }else {
            alert("你没有权限访问该页面")
          }
        }
      }
      else{
        next();
      }
    }
    else {//未登录
      next({
        path: '/login',
        query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }
  else {
    next();
  }
})
//后置
router.afterEach((to, from) => {
    console.log("路由后");
})
//导出路由
export default router;









